import datetime
from typing import List
from pydantic import Field, BaseModel, root_validator
from package.fastapi.schema import Page, BatchId, uuid7_hex


class Node(BaseModel):
    id: str = Field(title='节点ID', default_factory=uuid7_hex)
    name: str = Field('node', title='节点名')
    class Config:
        extra = 'allow'


class Relation(BaseModel):
    id: str = Field(title='关系ID', default_factory=uuid7_hex)
    name: str = Field('', title='关系名')

    start_node_id: str = Field(..., title='源节点ID')
    end_node_id: str = Field(..., title='目标节点ID')
    
    class Config:
        extra = 'allow'


class GraphElements(BaseModel):
    nodes: List[Node] = Field(..., title='') 
    relations: List[Relation] = Field(..., title='') 

    class Config:
        orm_mode = True


class EditGraph(BaseModel):
    """修改记录"""
    description: str = Field(None, title='描述')
    configure: dict = Field(None, title='配置')


class CreateGraph(EditGraph):
    """创建记录"""
    name: str = Field(..., title='名称')


class GraphOut(CreateGraph):
    """返回数据不返回密码"""
    id: str = Field(..., title='记录ID')
    nodes: List[Node] = Field(..., title='')
    relations: List[Relation] = Field(..., title='')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        extra = 'allow'
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class GraphExport(CreateGraph):
    """返回数据不返回密码"""
    id: str = Field(title='记录ID', default=uuid7_hex)
    nodes: List[Node] = Field(..., title='')
    relations: List[Relation] = Field(..., title='')

    class Config:
        orm_mode = True
        extra = 'allow'
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageGraphs(Page):
    """获取页码条目"""
    data: List[GraphOut] = Field(..., title='')
